const express = require("express");
const Result = require('../models/Result')
const community = require('../web_services/community');
const { md5 , decoded ,checkMobile } = require("../utils");
const { PWD_SALT , PRIVATE_KEY , USER_JWT_EXPIRED } = require("../utils/constant");
const jwt = require("jsonwebtoken")
const Boom = require('boom');
const { validationResult } = require("express-validator")

const router = express.Router();

Date.prototype.Format = function (fmt) { // author: meizz
	var o = {
		"M+": this.getMonth() + 1, // 月份
		"d+": this.getDate(), // 日
		"h+": this.getHours(), // 小时
		"m+": this.getMinutes(), // 分
		"s+": this.getSeconds(), // 秒
		"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
		"S": this.getMilliseconds() // 毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
}



router.post('/getCategoryList',function(req,res,next){//获取分类列表
	let data = req.body	
	community.getCategoryList(data).then(list=>{
		new Result(list,'查询成功').success(res)
	})
})

router.post('/getCategory',function(req,res,next){//获取分类详情
	let data = req.body
	const decode = decoded(req)

	community.getCategory(data).then(list=>{
		new Result(list,'查询成功').success(res)
	})

	if(decode && decode.uid){//更新分类浏览
		data.uid = decode.uid
		data.updated_at = new Date().Format("yyyy-MM-dd hh:mm:ss")
		community.updateLatelyCategory(data).then(list=>{
			
		})
	}

	
})

router.post('/getLatelyList',function(req,res,next){//获取分类列表
	let data = req.body	
	const decode = decoded(req)
	if(decode && decode.uid){//更新分类浏览
		data.uid = decode.uid
		community.getLatelyList(data).then(list=>{
			new Result(list,'查询成功').success(res)
		})
	}
})










module.exports = router